#ifndef SOCKS4A_H
#define SOCKS4A_H
#include <memory>
#include <unordered_map>
#include <mutex>
#include "./muduo-udns/Resolver.h"
#include "muduo/net/EventLoop.h"
#include "tunnel.h"
typedef std::shared_ptr<Tunnel> TunnelPtr;
/**
 * @brief 这是socks4a server
 *
 */
class Socks4a :public TcpServer {
 public:
  Socks4a(EventLoop* loop, const InetAddress& listenAddr);
  void start();
  void onServerConnection(const TcpConnectionPtr& conn);
  void onServerMessage(const TcpConnectionPtr& conn, Buffer* buf, muduo::Timestamp);

 private:
  void codecSocks4(const TcpConnectionPtr& conn, Buffer* buf,
                                           const char* data, size_t len);
  void farwardConnect(const TcpConnectionPtr& conn, Buffer* buf, const char* where,
                      const InetAddress& serverAddr);
 private:
  muduo::net::Resolver resolver_;
  std::mutex mutex_;
  std::unordered_map<std::string, TunnelPtr> tunnels_;  // 每一个conn->name 到Tunnel的映射
};
#endif